package com.zhong.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zhong.auth.application.convert.AuthRoleDTOConvert;
import com.zhong.auth.application.dto.AuthRoleDTO;
import com.zhong.auth.common.entity.Result;
import com.zhong.auth.domain.bo.AuthRoleBO;
import com.zhong.auth.domain.service.AuthRoleDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色controller
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class AuthRoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.add.authRoleDTO: {}", JSON.toJSONString(authRoleDTO));
            }
            //数据校验
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()),"角色名称不能为空！！！");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()),"角色key不能为空！！！");


            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE.convertDTOToBO(authRoleDTO);


            return Result.ok(authRoleDomainService.add(authRoleBO));
        }catch (Exception e){
            log.error("AuthRoleController.add.error: {}", e.getMessage(),e);
            return Result.fail("新增角色失败");
        }
    }

    /**
     * 删除角色
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.add.authRoleDTO: {}", JSON.toJSONString(authRoleDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色Id不能为空！！！");


            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE.convertDTOToBO(authRoleDTO);


            return Result.ok(authRoleDomainService.delete(authRoleBO));
        }catch (Exception e){
            log.error("AuthRoleController.add.error: {}", e.getMessage(),e);
            return Result.fail("删除角色失败");
        }
    }
    /**
     * 修改角色
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO){
        try {
            if (log.isInfoEnabled()){
                log.info("AuthRoleController.update.authRoleDTO: {}", JSON.toJSONString(authRoleDTO));
            }
            //数据校验
            Preconditions.checkNotNull(authRoleDTO.getId(),"角色Id不能为空！！！");


            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE.convertDTOToBO(authRoleDTO);


            return Result.ok(authRoleDomainService.update(authRoleBO));
        }catch (Exception e){
            log.error("AuthRoleController.add.error: {}", e.getMessage(),e);
            return Result.fail("修改角色失败");
        }
    }




}
